import store from '@/vuex/mutations'
let that = store.state.that
let validate = {
  toastObj: null,
  checkVliate: (arr) => {
    let status = 1
    for (let i = 0; i < arr.length; i++) {
      if (status) {
        let vs = null
        if (arr[i]['value'].length === 1) {
          vs = validate[arr[i]['nameFun']](arr[i]['value'][0])
        } else {
          vs = validate[arr[i]['nameFun']](arr[i]['value'])
        }
        status = vs.status
      }
    }
    return status
  },
  msg: (msg, status) => {
    if (validate.toastObj) validate.toastObj.close()
    if (!status) {
      validate.toastObj = that.$toast({
        position: 'center',
        message: msg,
        duration: 1000
      })
    }
  },
  statuCode: (status, msg) => {
    validate.msg(msg, status)
    return {
      status: status,
      msg: msg || ''
    }
  },
  // 手机
  isPhone (value) {
    let el = /^1[3|4|7|5|8][0-9]\d{8}$/.test(value)
    if (!value) {
      return validate.statuCode(0, '手机号码不能为空！')
      // callback(new Error('手机号码不能为空！'))
    } else if (!el) {
      return validate.statuCode(0, '手机号码格式不正确！')
    } else {
      return validate.statuCode(1, '')
    }
  },
  /**
   * 邮件
   */
  isEmail (value) {
    // eslint-disable-next-line no-useless-escape
    var el = /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/.test(value)
    if (!value) {
      return validate.statuCode(0, '邮箱格式请输入邮箱！不正确！')
    } else if (!el) {
      return validate.statuCode(0, '邮箱格式不正确！')
    } else {
      return validate.statuCode(1, '正确')
    }
  },
  /**
   * 密码设置确认
   * vinny
   */
  password (value) {
    if (!value) {
      return validate.statuCode(0, '请输入密码')
    } else {
      return validate.statuCode(1)
    }
  },
  checkpwd (value) {
    if (!value[0]) {
      return validate.statuCode(0, '请输入密码')
    } else if (value[0] !== value[1]) {
      return validate.statuCode(0, '输入密码不一致')
    } else {
      return validate.statuCode(1)
    }
  }
}
export {
  validate
}
